package com.cjzuji.rental.Bean;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpExample {
    public static String sendCurlRequest(String url, String method, String body) {
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json"); // 根据需要修改媒体类型
        RequestBody requestBody = (body != null && !body.isEmpty()) ? RequestBody.create(body, mediaType) : null;
        Request request = new Request.Builder()
                .url(url)
                .method(method, requestBody) // 设置请求方法和可选的请求体
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string(); // 返回响应体内容
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}